//
//  NetworkAccessModel.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-6-4.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "NetworkAccessManager.h"
#import "TimeoutAFHTTPRequestSerializer.h"
#import <UIActivityIndicatorView+AFNetworking.h>

#define kTimeoutInterval        15
#define kUpLoadTimeoutInterval  60


NSString *const NETWORK_INTERRUPT_NOTIFIACTION = @"network_interrupt_notification";

@implementation NetworkAccessManager


+(AFHTTPRequestOperationManager *)shareManager
{
    return [NetworkAccessManager shareManagerUseCache:NO];
    
}

+(AFHTTPRequestOperationManager *)shareManagerUseCache:(BOOL)useLocalCached{
    static dispatch_once_t pred;
    static AFHTTPRequestOperationManager *manager = nil;
    
    dispatch_once(&pred, ^{
        manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl]];
        
        [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
            if (status == AFNetworkReachabilityStatusNotReachable || status == AFNetworkReachabilityStatusUnknown) {
                // no internet
                [manager.operationQueue cancelAllOperations];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:NETWORK_INTERRUPT_NOTIFIACTION object:nil];
            }
        }];
        [manager.reachabilityManager startMonitoring];

    });
    
    // 是否从本地缓存加载
    if (useLocalCached) {
        manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
    }else{
        manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    }
    return manager;
}
@end
